package com.deyuanyun.pic.settings.service.prvlg.impl;

import com.deyuanyun.pic.common.util.PassWordUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.prvlg.RoleGrant;
import com.deyuanyun.pic.settings.controller.dto.prvlg.UserQuery;
import com.deyuanyun.pic.settings.domain.prvlg.User;
import com.deyuanyun.pic.settings.mapper.prvlg.UserMapper;
import com.deyuanyun.pic.settings.service.prvlg.UserMgrService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 权限模块 “用户”服务接口实现类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-28
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class UserMgrServiceImpl implements UserMgrService {

	@Resource
	private UserMapper userMapper;

	@Override
	public Page<User> query(UserQuery userQuery) {
		return null;
	}

	@Override
	public User find(String id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int edit(User user) {
		if (StringUtils.isNotNullEmpty(user.getId())){
			return userMapper.updateByPrimaryKeySelective(user);
		} else {
			user.setPwd(PassWordUtil.initPassword(user.getUsername()));
			return userMapper.insertSelective(user);
		}
	}

	@Override
	public int delete(String id) {
		User user = this.find(id);
		if (user != null) {
			user.setDeleted(Short.valueOf("1"));
			return this.edit(user);
		}
		return 0;
	}

	@Override
	public int grant(RoleGrant roleGrant) {
		return 0;
	}

	@Override
	public int updateState(String userId, String state) {
		return 0;
	}

	@Override
	public int resetPwd(String[] ids) {
		int count = 0;
		for (String id : ids){
			User user = this.find(id);
			if (user != null){
				user.setPwd(PassWordUtil.initPassword(user.getUsername()));
				this.edit(user);
				count++;
			}
		}
		return count;
	}
}
